/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Joueur.java
 *
 * Created on 23 sept. 2011, 15:42:44
 */
package vue;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.net.URL;
import java.util.Observable;
import java.util.Observer;
import javax.swing.ImageIcon;
import model.gestion_jeu.Joueur;

/**
 *
 * @author NABIL
 */
public class JoueurVue extends javax.swing.JPanel implements Observer {
    protected Image image;
    private boolean tour;
    private String nom;
    /** Creates new form Joueur */
    public JoueurVue(String couleur, Joueur j) {
        initComponents();
        this.nom = j.getNom();
        nomLabel.setText("<html><div style=\"padding:10;\">"+nom+": "+j.getNbPiece()+"</div><html>");
        this.setLayout(new BorderLayout());
        this.add(this.nomLabel,BorderLayout.CENTER);
        URL file = null;
        if(couleur.equals("blanc"))
            file = getClass().getClassLoader().getResource("vue/resources/pionbl.png");
        else
            file = getClass().getClassLoader().getResource("vue/resources/pionnoir.png");
       ImageIcon im = new ImageIcon(file);
        image = im.getImage();
       tour = j.isMonTour();
       
       j.addObserver(this);
       
    }
    
    public String getNom()
    {
        return nom;
    }
    
    public boolean isTour()
    {
        return tour;
    }
    public void setTour(boolean t)
    {
        this.tour = t;
    }

    @Override
     public void paintComponent(Graphics g){
        URL file= null;
       ImageIcon im = null;
       
        if(tour)
        {
            g.setColor(new Color(255,255,153));
            g.fillRect(this.getWidth()/4, 0, this.getWidth()/2, this.getHeight());
            file = getClass().getClassLoader().getResource("vue/resources/joueur2.png");
            im = new ImageIcon(file);
        }
        else
        {
            file = getClass().getClassLoader().getResource("vue/resources/joueur3.png");
            im = new ImageIcon(file);
        }
         g.drawImage(im.getImage(),0, 0, this.getWidth(), this.getHeight(), null, null);
      
        g.drawImage(image,2*this.getWidth()/3, this.getHeight()/4, this.getHeight()/2, this.getHeight()/2, null, null);
    }
     
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        nomLabel = new javax.swing.JLabel();

        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(vue.DamierApp.class).getContext().getResourceMap(JoueurVue.class);
        nomLabel.setFont(resourceMap.getFont("nomLabel.font")); // NOI18N
        nomLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        nomLabel.setText(resourceMap.getString("nomLabel.text")); // NOI18N
        nomLabel.setName("nomLabel"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(151, 151, 151)
                .addComponent(nomLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
                .addGap(149, 149, 149))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(nomLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel nomLabel;
    // End of variables declaration//GEN-END:variables

    public void update(Observable o, Object arg) {
        try
        {
            if(o instanceof Joueur)
            {
                Joueur j = (Joueur)o;
                this.tour = j.isMonTour();
                nomLabel.setText("<html><div style=\"padding:10;\">"+nom+": "+j.getNbPiece()+"</div><html>");
                this.repaint();
            }
            
        }catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}
